package com.zjitc.pigspring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zjitc.pigspring.bean.po.Admin;
import com.zjitc.pigspring.bean.po.User;
import com.zjitc.pigspring.common.Codes;
import com.zjitc.pigspring.common.Result;
import com.zjitc.pigspring.service.AdminService;
import com.zjitc.pigspring.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/getAll")
    public Result<List<Admin>> getAll(){
        List<Admin> admins = adminService.list();
        if(admins!=null){
            return Result.success(admins);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PostMapping("/addOne")
    public Result addAdmin(@RequestBody Admin admin){
        QueryWrapper<Admin> wrapper = new QueryWrapper<Admin>()
                .eq("username",admin.getUsername());
        Admin one = adminService.getOne(wrapper);
        if (one!=null){
            return Result.error(Codes.USER_EXISTS);
        }
        boolean save = adminService.save(admin);
        if (save){
            return Result.success(Codes.ADD_YES);
        }else{
            return Result.error(Codes.ADD_NO);
        }
    }

    @DeleteMapping("/deleteOne/{id}")
    public Result deleteAdmin(@PathVariable Integer id){
        boolean flag = adminService.removeById(id);
        if (flag){
            return Result.success(Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @PostMapping("/deleteAll")
    public Result deleteAdmins(@RequestBody List<Integer> ids){

        boolean flag = adminService.removeBatchByIds(ids);
        if (flag){
            return Result.success(Codes.DELETE_YES);
        }else{
            return Result.error(Codes.DELETE_NO);
        }
    }

    @GetMapping("/selectOne/{id}")
    public Result<Admin> selectAdminById(@PathVariable Integer id){
        Admin admin = adminService.getById(id);
        if(admin!=null){
            return Result.success(admin);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }

    @PutMapping("/updateOne")
    public Result updateAdmin(@RequestBody Admin admin){
        QueryWrapper<Admin> wrapper = new QueryWrapper<Admin>()
                .eq("username",admin.getUsername());
        Admin one = adminService.getOne(wrapper);
        if (one!=null && !one.getUsername().equals(admin.getUsername())){
            return Result.error(Codes.USER_EXISTS);
        }
        boolean flag = adminService.updateById(admin);
        if (flag){
            return Result.success(Codes.UPDATE_YES);
        }else{
            return Result.error(Codes.UPDATE_NO);
        }
    }

    @PostMapping("/conditionQuery")
    public Result<List<Admin>> queryType(@RequestBody Admin admin){

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<Admin>()
                .like(StringUtils.isNotBlank(admin.getName()),"name",admin.getName());
        List<Admin> list = adminService.list(queryWrapper);
        if(list!=null){
            return Result.success(list);
        }else{
            return Result.error(Codes.GET_INFO_FAILED);
        }
    }
}
